home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / ask_bat.zip / ASK_BAT.C next >
C/C++ Source or Header  |  1989-02-17  |  15KB  |  334 lines

  1.                      /*********************/
  2.                      /* Program ASK_BAT.C */
  3.                      /*********************/
  4.  
  5. /* Copyright 1989 Floyd D. Pretz Sr.
  6.  *                5834 Spaulding St.
  7.  *                Omaha, NE  68104
  8.  *
  9.  * Purpose: To parse Console input within a batch file and return with
  10.  *          with an appropriate DOS ERRORLEVEL, and/or echo text to the
  11.  *          console with cursor control.
  12.  *
  13.  * Input:  DOS command line parameters.
  14.  *
  15.  * Output: Modified ERRORLEVEL for batch testing.
  16.  *
  17.  * Description: ask_bat was written with the intention of providing
  18.  *              a method to pause for a few seconds within a batch
  19.  *              file (especially autoexec) to get an option from
  20.  *              the operator that would determine the next action
  21.  *              that the batch file would perform.  If the operator
  22.  *              was unavailable, some default option could be assumed
  23.  *              then batch processing continued.
  24.  *
  25.  *  Use:  ask_bat [ 'string' errlev ... ] [ /T:nnn ] [ /H ] [ /F ] [ /P prompt ]
  26.  *
  27.  *        string - any character string to match from the keyboard.
  28.  *        errlev - the ERRORLEVEL to exit with if preceding string selected.
  29.  *        /T:nnn - maximum time to wait for keyboard activity.
  30.  *            /H - hot key switch (On). attempt to match on one keystroke.
  31.  *            /F - force match switch (On).  'string' must be matched.
  32.  *            /P - prompt text follows.
  33.  *        prompt - text to display prior to keyboard parsing.
  34.  *
  35.  *        Notes: - string-errlev pairs, if any, must occur first in the
  36.  *                 parameter list.
  37.  *               - parameters are space delimited.
  38.  *               - switches may occur in any order following the string-errlev
  39.  *                 pairs.
  40.  *               - prompt or string parameters must be enclosed in double
  41.  *                 quotes (") if either contains a space character.
  42. */
  43.  
  44.         #include <stdio.h>
  45.         #include <stdlib.h>
  46.         #include <time.h>
  47.         #include <string.h>
  48.         #include <stdlib.h>
  49.         #include <conio.h>
  50.  
  51.         #define false 0
  52.         #define true 1
  53.  
  54.         void main(int argc, char *argv[])
  55.  
  56.         {
  57.         clock_t start, end, elapsed;            /* define timer variabls */
  58.         char response[129];                     /* storage for keystroks */
  59.         int timed, hotkey, forced;              /* logical flags         */
  60.         int i, indx, lastpair;                  /* miscellaneous         */
  61.         int maxtime, errlev, keys;              /* miscellaneous         */
  62.         void ask_hlp();                         /* define usage routine  */
  63.         void ask_hlp_big();                     /* define help routine   */
  64.  
  65. /*
  66.  *                    
  67.  *  Main Program Body 
  68.  *                    
  69. */
  70.  
  71. /*
  72.  *  Check for "?" parameter or absence of
  73.  *  parameters and give appropriate help message.
  74. */
  75.         if (argc==1) { ask_hlp();               /* if no command args */
  76.                        i=getch();               /* print usage, get a */
  77.                        exit(0);   }             /* keystroke & exit   */
  78.         if (!stricmp("?",argv[1]))              /* if ? command arg   */
  79.                      { ask_hlp_big();           /* print help & exit  */
  80.                        exit(0);   }
  81. /*
  82.  *  Initialize local variables
  83. */
  84.     response[0] = 0;
  85.         start = clock();                        /* get initial time stamp */
  86.         timed = hotkey = forced = false;        /* default options false */
  87.         keys = 0;                               /* Keyboard buffer index */
  88.         errlev=-1;                              /* -1 indicates no match */
  89.         lastpair=argc;                          /* last string-errlev    */
  90.  
  91. /*
  92.  * Process the command line by looping
  93.  * through the argument list looking for 
  94.  * switches and setting logic flags.
  95. */
  96.         for (indx = 1; indx <= argc-1; indx++)
  97.            {                                
  98.             if (!strnicmp("/ ",argv[indx],1)) {        /* switch parameter?  */
  99.                if (lastpair==argc) lastpair=indx-1;    /* set lastpair index */
  100.                if (!stricmp("/H",argv[indx]))                 /* Hot Key ? */
  101.                   hotkey=true;
  102.                else if (!stricmp("/F",argv[indx]))            /* Forced  ? */
  103.                   forced=true;
  104.                else if (!strnicmp("/T:",argv[indx],3))        /* Timed   ? */
  105.                   { timed=true;
  106.                     maxtime=atoi(argv[indx]+3); }
  107.                else if (!stricmp("/P",argv[indx]))            /* Prompt  ? */
  108.                   { if (argc >= indx)
  109.                        printf(argv[++indx]); }
  110.                else
  111.                   printf("Invalid switch '%s'\n",argv[indx]); /* ?????? ?  */
  112.                }
  113.            }
  114. /*
  115.  *  loop indefinitely until time exceeded while gathering keystrokes.
  116.  *  continue looping until parameter list satisfied (errlev != -1).
  117. */
  118.  
  119.         do {
  120.            if (!kbhit()) {                          /* Keystroke waiting?   */
  121.               if (timed) {                          /* No keyboard activity */
  122.                  end=clock();                       /* get & check time     */
  123.                  elapsed=(end-start)/13;            /* CLK_TCK not used     */
  124.                  if (elapsed > maxtime) {           /* Timeout!             */
  125.                     errlev=0;
  126.                     if (lastpair > 0) {             /* assume match on 1st  */
  127.                        printf (argv[1]);            /* string-errlev pair   */
  128.                        errlev=atoi(argv[2]);
  129.                     }
  130.                  }
  131.               }
  132.            }
  133. /*
  134.  *  Key Hit!   - Process non-ascii as a delete key.
  135.  *             - Trap Hot keys
  136.  *             - Buffer other keys
  137.  *             - & reset clock
  138. */
  139.            else {
  140.               i=getch();
  141.               if (!i || i==8) {                 /* if null/delete then  */
  142.                  if (!i) i=getch();             /* process as delete    */
  143.                  if (keys) {
  144.                     --keys;                     /* backup string end    */
  145.                     response[keys]=0;                                     
  146.                     printf("\b \b");
  147.                  }
  148.               }
  149.               else {
  150.                  if (i != 13) {                 /* Buffer keystroke &  */
  151.                     response[keys]=i;           /* display character   */
  152.                     if (!hotkey) {              /* if not hot key mode */
  153.                        keys++;
  154.                        response[keys]=0;        /* string terminator   */
  155.                        putchar(i);
  156.                     }
  157.                  }
  158. /*
  159.  *  Hotkey! or <RETURN> key!
  160. */  
  161.                  if (hotkey||(i==13)) {
  162.                     if (lastpair==0) errlev=0;
  163. /*
  164.  *  Loop through string-errlev pairs looking for a match
  165. */
  166.                     for (indx = 1; indx < lastpair; indx=indx+2) {
  167. /*
  168.  *  Check hotkey
  169. */
  170.                        if (hotkey && !strnicmp(response,argv[indx],1)) {
  171.                           errlev=atoi(argv[indx+1]);
  172.                           printf(argv[indx]); 
  173.                        }
  174. /*
  175.  *  Check non-hotkey strings
  176. */
  177.                        else if (!stricmp(response,argv[indx]))
  178.                           errlev=atoi(argv[indx+1]);
  179.                     }
  180. /*
  181.  *  Process a simple <RETURN> as a default
  182. */
  183.                     if (!forced && errlev==-1) { 
  184.                        if (i==13 && keys==0) {
  185.                           printf(argv[1]);
  186.                           errlev=atoi(argv[2]);
  187.                        }
  188.                     }
  189. /*
  190.  *  Bell (Beep) if no match or miss match
  191. */
  192.                     if (errlev==-1) printf("\a");
  193.                  }
  194.               }
  195.               start=clock(); }
  196. /*
  197.  *  End of keyboard processing loop
  198. */
  199.            } while (errlev < 0 );
  200.         printf("\n");
  201.         exit(errlev);
  202.         }
  203.  
  204. /*                                */
  205. /*        subroutine ask_hlp      */
  206. /*